<?php

class CpshippingController extends Core_Controller_Action 
{
	
	public function indexAction() {
		$this->_forward( 'facility', 'cpshipping' );
	}
	
	public function facilityAction() {
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$Facility = new Facility();
		
		$pagerLayout = $Facility->getListWithPager( $page, $this->view->baseUrl() );
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
		
		$this->assignType( 0 );
	}
	
	public function itemfacilityAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		$Facility = new Facility();
		
		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();
			
			foreach( $post as $key => $value ) {

				if( strlen( $value ) > 0 ) {
					$this->view->{$key} = $value;
				}
			}

			if( empty( $post['description_pl'] ) ) {
				$this->view->err_description_pl = 'Podaj opis';
			} else {

				if( $id ) {
					$item = $Facility->getItem( $id )->getFirst();
					foreach( $post as $key => $value ) {
	
						if( strlen( $value ) > 0 ) {
							$item->{$key} = $value;
						}
					}
					$item->save();

					$this->_redirect( 'cpshipping/facility/oper/2' );
				} else {
					foreach( $post as $key => $value ) {
	
						if( strlen( $value ) > 0 ) {
							$Facility->{$key} = $value;
						}
					}
					$Facility->save();
					
					$this->_redirect( 'cpshipping/facility/oper/1' );
				}
			}

		} else {
			if( $id ) {
				$item = $Facility->getItem( $id )->toArray();

				foreach( $item[0] as $key => $value ) {
	
					$this->view->{$key} = $value;
				}
			}
		
		}
		
		$this->assignType( 0 );
	}
	
	public function deletefacilityAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$Facility = new Facility();
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id > 0 ) {
			$Facility->deleteItem( $id );
			$this->_redirect( 'cpshipping/facility/oper/4' );
		} else {
			$this->_redirect( 'cpshipping/facility/oper/3' );
		}
	}
	
	public function courierAction() {
		$Settings = new Settings();
		
		if( $this->_request->isPost() ) {
			$post = $this->_request->getPost();

			$courier = $Settings->getItemByName( 'courier' )->getFirst();
			$courier->value = number_format( floatval( $post['courier'] ), 2, '.', '' );
			$courier->save();
			
			$r1 = $Settings->getItemByName( 'r1' )->getFirst();
			$r1->value = $post['r1'];
			$r1->save();
			
			$r2 = $Settings->getItemByName( 'r2' )->getFirst();
			$r2->value = $post['r2'];
			$r2->save();
			
			$r3 = $Settings->getItemByName( 'r3' )->getFirst();
			$r3->value = $post['r3'];
			$r3->save();
			
			$r4 = $Settings->getItemByName( 'r4' )->getFirst();
			$r4->value = $post['r4'];
			$r4->save();
			
			$r5 = $Settings->getItemByName( 'r5' )->getFirst();
			$r5->value = $post['r5'];
			$r5->save();
			
			$this->view->courier = number_format( floatval( $post['courier'] ), 2, '.', '' );
			$this->view->r1 = $post['r1'];
			$this->view->r2 = $post['r2'];
			$this->view->r3 = $post['r3'];
			$this->view->r4 = $post['r4'];
			$this->view->r5 = $post['r5'];
		} else {
			$courier = $Settings->getItemByName( 'courier' )->getFirst()->toArray();
			$r1 = $Settings->getItemByName( 'r1' )->getFirst()->toArray();
			$r2 = $Settings->getItemByName( 'r2' )->getFirst()->toArray();
			$r3 = $Settings->getItemByName( 'r3' )->getFirst()->toArray();
			$r4 = $Settings->getItemByName( 'r4' )->getFirst()->toArray();
			$r5 = $Settings->getItemByName( 'r5' )->getFirst()->toArray();
	
			$this->view->courier = number_format( floatval( $courier['value'] ), 2, '.', '' );
			$this->view->r1 = $r1['value'];
			$this->view->r2 = $r2['value'];
			$this->view->r3 = $r3['value'];
			$this->view->r4 = $r4['value'];
			$this->view->r5 = $r5['value'];
		}
		
		$this->assignType( 1 );
	}
	
	
	//=====================pack==================================
	
	public function packAction() {
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$Pack = new Pack();
		
		$pagerLayout = $Pack->getListWithPager( $page, $this->view->baseUrl() );
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
		
		$this->assignType( 3 );
	}
	
	public function itempackAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		$Pack = new Pack();
		
		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();
			
			foreach( $post as $key => $value ) {
	
				if( strlen( $value ) > 0 ) {
					$this->view->{$key} = $value;
				}
			}

			if( empty( $post['name'] ) ) {
				$this->view->err_name = 'Podaj nazwę';
			} else {

				if( $id ) {
					$item = $Pack->getItem( $id )->getFirst();
					$list = array();
					$i = 0;
					
					foreach( $post as $key => $value ) {
	
						if( strlen( $value ) > 0 ) {
							if(substr($key,0,6) == 'weight' ||  substr($key,0,5) == 'price')
							{
							$list[] = $key;
							$list[] = $value;
							}
							else
								$item->{$key} = $value;
						}
					}
				$list2 = array();
					$a = 0;
					$a_k = '';
					$b = 0;
					$b_k = '';
					$price2 = 0;
					$price2_k  = '';
					
					for($i = 0; $i < count($list);)
					{
					
					$id = substr($list[$i],strlen($list[$i])-1,1);
					
					
					if(substr($list[$i],0,6) == 'weight')
					{
						if(substr($list[$i],6,2) == 'od')
						{
						$a_k = $list[$i];
						$a = $list[$i+1];
						$i+=2;
						
						if($i <= count($list)-1)
						{
						if(substr($list[$i],0,6) == 'weight' && substr($list[$i],strlen($list[$i])-1,1) == $id)
						{
						$b_k = $list[$i];
						$b = $list[$i+1];
						$i+=2;
						
						}
						else
						{	
							continue;
						}
						
						
						}
						else
						{	
							break;
						}
						if($i <= count($list)-1)
						{
						
						if(substr($list[$i],0,5) == 'price' && substr($list[$i],strlen($list[$i])-1,1) == $id)
						{
							$price2_k = $list[$i];
							$price2 = $list[$i+1];
							$i+=2;
						}
						else
						{
							
							continue;
						}
							
						
						}
						else
						{
							
							break;
						}
						
						}
						else
						{
							$i+=2;
							continue;
						}
						
						
					}
					else
					{
					$i += 2;
					continue;
					}
					
					
						$list2[] = $a_k;
						$list2[] = $a;
						$list2[] = $b_k;
						$list2[] = $b;
						$list2[] = $price2_k;
						$list2[] = $price2;
					
						
					}
					
					
					$serializedArray= serialize($list2);
					$item->price = $serializedArray;
					$item->save();

					$this->_redirect( 'cpshipping/pack/oper/2' );
				} else {
					$list = array();
					foreach( $post as $key => $value ) {
	
						if( strlen( $value ) > 0 ) {
							if(substr($key,0,6) == 'weight' ||  substr($key,0,5) == 'price')
							{
							$list[] = $key;
							$list[] = $value;
							}
							else
								$Pack->{$key} = $value;
						}
					}
					
					$list2 = array();
					$a = 0;
					$a_k = '';
					$b = 0;
					$b_k = '';
					$price2 = 0;
					$price2_k  = '';
					
					for($i = 0; $i < count($list);)
					{
					
					$id = substr($list[$i],strlen($list[$i])-1,1);
					
					
					if(substr($list[$i],0,6) == 'weight')
					{
						if(substr($list[$i],6,2) == 'od')
						{
						$a_k = $list[$i];
						$a = $list[$i+1];
						$i+=2;
						
						if($i <= count($list)-1)
						{
						if(substr($list[$i],0,6) == 'weight' && substr($list[$i],strlen($list[$i])-1,1) == $id)
						{
						$b_k = $list[$i];
						$b = $list[$i+1];
						$i+=2;
						
						}
						else
						{	
							continue;
						}
						
						
						}
						else
						{	
							break;
						}
						if($i <= count($list)-1)
						{
						
						if(substr($list[$i],0,5) == 'price' && substr($list[$i],strlen($list[$i])-1,1) == $id)
						{
							$price2_k = $list[$i];
							$price2 = $list[$i+1];
							$i+=2;
						}
						else
						{
							
							continue;
						}
							
						
						}
						else
						{
							
							break;
						}
						
						}
						else
						{
							$i+=2;
							continue;
						}
						
						
					}
					else
					{
					$i += 2;
					continue;
					}
					
					
						$list2[] = $a_k;
						$list2[] = $a;
						$list2[] = $b_k;
						$list2[] = $b;
						$list2[] = $price2_k;
						$list2[] = $price2;
					
						
					}
					
					$serializedArray= serialize($list2);
					$Pack->price = $serializedArray;
					$Pack->save();
					
					$this->_redirect( 'cpshipping/pack/oper/1' );
				}
			}

		} else {
			if( $id ) {
				$item = $Pack->getItem( $id )->toArray();

				foreach( $item[0] as $key => $value ) {
					if($key == 'price')
					{
						$serial= unserialize($value);
						
						for($i=0;$i<count($serial);)
						{
							$this->view->{$serial[$i]} = $serial[$i+1];
							$i=$i+2;
						}
						
					}
					else
						$this->view->{$key} = $value;
				}
			}
		
		}

		$this->assignType( 3 );
	}
	
	public function deletepackAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$Pack = new Pack();
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id > 0 ) {
			$Pack->deleteItem( $id );
			$this->_redirect( 'cpshipping/pack/oper/4' );
		} else {
			$this->_redirect( 'cpshipping/pack/oper/3' );
		}
	}
	
	//=========================================================
	
	
	
	
	
	public function freeshippingAction() {
			$Settings = new Settings();
		
		if( $this->_request->isPost() ) {
			$post = $this->_request->getPost();

			$freeprice = $Settings->getItemByName( 'freeprice' )->getFirst();
			$freeprice->value = number_format( floatval( $post['freeprice'] ), 2, '.', '' );
			$freeprice->save();

			$this->view->freeprice = number_format( floatval( $post['freeprice'] ), 2, '.', '' );
		} else {
			$freeprice = $Settings->getItemByName( 'freeprice' )->getFirst()->toArray();
	
			$this->view->freeprice = number_format( floatval( $freeprice['value'] ), 2, '.', '' );
		}

		$this->assignType( 2 );
	}
	
	public function assignType( $type ) {
		
		$this->view->type = $type;
		
		switch( $type ) {
			case 0 :
				$this->view->yih2 = 'Zarządzanie placówkami';
				break;
			case 1 :
				$this->view->yih2 = 'Zarządzanie kurierem';
				break;
			case 2 :
				$this->view->yih2 = 'Zarządzanie transportem';
				break;
			case 3 :
				$this->view->yih2 = 'Zarządzanie paczkami';
				break;
		}
	}
}